// pages/all_error/index.js
//引入 发送网络请求的方法
const app = getApp()
const root = app.globalData.url 
import { chooseVideo,uploadFile,showModal} from '../../utils/asyncwx.js'
const http = require('../../utils/http.js')
import regeneratorRuntime from '../../lib/runtime/runtime'
Page({

  /**
   * 页面的初始数据
   */
  
  data: {
    userInfo:{}, //用户信息对象
    tname:'',//真实姓名
    uid: null,
    role: null, 
    status:null,//用户状态
    videoList:null,
    size:null,
    uvideo:null,
    hash:null   
   
    
    
  },
  async DelVideo(e) {
    let res=await showModal({content:'确定要删除此视频吗？',showCancel:true})
    if (res.confirm) {
      //this.data.imgList.splice(e.currentTarget.dataset.index, 1);
      this.setData({
        videoList:null,
        size:null
      })
    }    
  },// end of DelVideo
  async dosubmitall(){
    //最后上传
    let uid = this.data.uid
    let uvideo = this.data.uvideo
    let size = this.data.size
    let hash=this.data.hash
    let url = 'Clock/add/';
    let data = {
       
        uid: uid,
        url: uvideo,
        size:size,
        hash:hash
        
      }
     let res=await http.post(url, data)
     
     console.log(res)
     
      if( res=='ok'){
        //入库成功
        await showModal({content:'打卡成功！',showCancel:false})
        //await showToast({title:'入库成功！',icon:'success'})
        wx.switchTab({
         url: '/pages/clock/index'
       })
      }
       
 
     
    
     
    // console.log(res)
  },
  async dosubmit(){
    //上传文件
    
    //let uid = this.data.uid
  
   
    let video = this.data.videoList
    try {    
      
      let res=await uploadFile(
        root+'file/uploadVideo',
        video,
        'uploadvideo',
        {}
      )
      console.log(res)
     //shipin=JSON.parse(res.data).data.imgurl //视频地址
    
     
        let hash=JSON.parse(res.data).data.hash
        let uvideo=JSON.parse(res.data).data.imgurl
        console.log(hash)
        console.log(uvideo)
        this.setData({
          uvideo,
          hash
        })      
        this.dosubmitall()
     
    }catch(err)
    {
      console.log('error'+err)
      await showModal({content:'上传错误！'+err,showCancel:false})  
    }
    
    
    

  },// end of dosubmit
  async submitForm(e) {
      
    let size=this.data.size
    let role =this.data.role  
   
    if (role != "1") {
     let res= await showModal({content:'请先完善您的个人信息！',showCancel:true})   
     if (res.confirm) {           
      wx.switchTab({
        url: '/pages/user/index'
      })
     }    
      return false;
    }
    //if (!status) {
     // await showModal({content:'用户被禁，请联系管理员！',showCancel:false}) 
    
     // return false;
   // } 
    if (!size) {
      await showModal({content:'请先选择视频！',showCancel:false}) 
    
      return false;
    } 
    if (size > 20) {
      await showModal({content:'视频最大允许20M，当前为'+ (size + 'M'),showCancel:false})
     
      return false;
    } 
    this.dosubmit()
   
  },
  async gettoken(){
    //从后台获取token
    let url = 'wx/login/'
    let data = {
      uid: this.data.uid,     
    }
    let res= await http.post(url,data)
    console.log(res)
    if(res){
      wx.setStorageSync('token',res)
    }
   
    
  },
  async handleChooseVideo (){
    //视频选择  res.tempFiles[0].tempFilePath  res.tempFilePath
    let res=await chooseVideo()   
    this.setData({
          videoList: res.tempFilePath,
          size: (res.size / (1024 * 1024)).toFixed(2)
    })

   
   
   
      console.log('videoList')
      console.log(this.data.videoList)
    
    
   
  }, //end of ChooseVideo
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
   
   
    
    
  },
 


  
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    //判断登录状态结束
    
    const userInfo=wx.getStorageSync("userInfo")
    const tname=wx.getStorageSync('tname')
    let uid = wx.getStorageSync('uid')   
    let role = wx.getStorageSync('role')
    let status = wx.getStorageSync('status')
    //console.log('role=',role)
    this.setData({
      userInfo,
      uid,
      role,
      tname,
      status
    })
    this.gettoken()
   
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})